﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using qiupeng.Public;
using System.IO;

using Ths.Framework;
using Ths.Components.Common;
using Ths.Components.Dictionary;
using Ths.Components.Dictionary.Entity;
using SafetyRecordData.Data.DAL;

public partial class DistressManage_HurtListSearch : Base
{
    Db List = new Db();
    public static string CountsLabel, AllPageLabel, CurrentlyPageLabel, SqlStrStart, SqlStrMid, SqlStr, SqlStrStartCount, SqlStrCount;
    DAHurtDistress dal = new DAHurtDistress();
    protected void Page_Load(object sender, EventArgs e)
    {
       

        if (!Page.IsPostBack)
        {
            BindAttribute();

            Bindquanxian();
            DataBindToGridview("order by id desc");
            if (GetRequestStr("islook") == "1")
            {
                this.Img_Search.Visible = false;
            }

        }
    }

    #region 初始化
    /// <summary>
    /// 权限绑定
    /// </summary>
    public void Bindquanxian()
    {
        List.QuanXianVis(Img_Show, "bbbb7b", Session["perstr"].ToString());
        List.QuanXianVis(Img_Search, "bbbb7e", Session["perstr"].ToString());


    }

    public void BindAttribute()
    {
        Img_Show.Attributes["onclick"] = "javascript:return updatecheck();";

        btnFirst.Attributes["onclick"] = "javascript:return showwait();";
        btnPrev.Attributes["onclick"] = "javascript:return showwait();";
        btnNext.Attributes["onclick"] = "javascript:return showwait();";
        btnLast.Attributes["onclick"] = "javascript:return showwait();";
        btnReturn.Attributes["onclick"] = "javascript:return chkyema();";
    }
    #endregion

    public void DataBindToGridview(string Sqlsort)
    {
        SqlStrStart = "select * from HurtDistress where 1=1";
        SqlStrStartCount = "select count(id) from HurtDistress where 1=1";


        SqlStrMid = string.Empty;
        SqlStrMid = CreateMidSql();


        switch (Session["ResponseRemark"].ToString())//根据目前所属的角色查询的
        {
            case "普通":
                SqlStrStart += " and status='55' and workernumber='" + Session["StaffNumer"] + "'  ";
                SqlStrStartCount += " and status='55'  and workernumber='" + Session["StaffNumer"] + "' ";
                break;
            case "区队":
                SqlStrStart += " and isnull(status,'0') in ('55') and  companyid in ('" + Session["bmqxa"].ToString().Replace(",", "','") + "') ";
                SqlStrStartCount += "  and isnull(status,'0') in ('55') or isnull(status,'0')='12') and  companyid in ('" + Session["bmqxa"].ToString().Replace(",", "','") + "') ";
                break;
            case "矿级":
                SqlStrStart += " and isnull(status,'0') in ('55') and  companyid in ('" + Session["bmqxa"].ToString().Replace(",", "','") + "')  ";
                SqlStrStartCount += "  and isnull(status,'0') in ('55')  and  companyid in ('" + Session["bmqxa"].ToString().Replace(",", "','") + "') ";
                break;
            case "局级":
                SqlStrStart += " and status='55'  ";
                SqlStrStartCount += " and status='55'";
                break;
        }

        SqlStr = SqlStrStart + SqlStrMid;//查询
        SqlStrCount = SqlStrStartCount + SqlStrMid;//查询个数

        string SQL_GetList_xs = "" + SqlStr + " " + Sqlsort + "";
        GridView1.DataSource = List.GetGrid_Pages_not(SQL_GetList_xs);
        GridView1.DataBind();

        string SQL_GetCount = SqlStrCount;
        CountsLabel = "" + List.GetCount(SQL_GetCount) + "";

        AllPageLabel = Convert.ToString(GridView1.PageCount);
        CurrentlyPageLabel = Convert.ToString(((int)GridView1.PageIndex + 1));

        btnFirst.CommandName = "1";
        btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());

        btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
        btnLast.CommandName = GridView1.PageCount.ToString();
    }





    private string CheckCbxDel()
    {
        string str = "0";
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }


    private string CheckCbxNameDel()
    {
        string str = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabNameVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }

    private string CheckCbxUpdate()
    {
        string str = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }

    public string CreateMidSql()
    {
        string str = string.Empty;
        if (Request.QueryString["key"] != null)
        {
            if (base.Request.QueryString["Name"] != null)
            {
                str = str + " and Company like '%" + this.List.GetFormatStr(base.Server.UrlDecode(base.Request.QueryString["Name"])) + "%'";
            }
            if (base.Request.QueryString["Username"] != null)
            {
                str = str + " and name like '%" + this.List.GetFormatStr(base.Server.UrlDecode(base.Request.QueryString["Username"])) + "%'";
            }
            if (base.Request.QueryString["Type"] != null)
            {
                str = str + " and  (isnull(status,'0'))='" + this.List.GetFormatStr(base.Server.UrlDecode(base.Request.QueryString["type"])) + "'";
            }
            if (base.Request.QueryString["date"] != null)
            {
                str = str + " and happentime>='" + base.Request.QueryString["date"].ToString() + " 00:00:00' and   happentime<='" + base.Request.QueryString["date"].ToString() + " 23:59:59'";
            }

        }
        return str;

    }

    protected void Img_Update_Click(object sender, ImageClickEventArgs e)
    {

        Response.Redirect("Hurt_update.aspx?appid=" + CheckCbxUpdate() + "");

    }

    protected void Img_Add_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Hurt_update.aspx?IsNew=1");
    }

    public string GetStatus(string strCode)
    {
        switch (strCode)
        {
            case "":
                return "暂存";
                break;
            case "0":
                return "暂存";
                break;
            case "1":
                return "提交";
                break;
            case "12":
                return "退回";
                break;
            case "55":
                return "审核通过";
                break;
            default:
                break;
        }
        return "";
    }


    protected void Img_Show_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Hurt_update.aspx?appid=" + CheckCbxUpdate() + "&view=1&islook=" + GetRequestStr("islook") + "");
    }



    #region GridView基本信息
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataControlRowType itemType = e.Row.RowType;
        if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
        {
            e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";

            try
            {
                e.Row.Cells[8].Text = GetStatus(e.Row.Cells[8].Text.Replace("&nbsp;", ""));
            }
            catch (Exception er) { }

            Label lblHurtbody = (Label)e.Row.FindControl("lblHurtbody");
            if (lblHurtbody != null)
            {
                lblHurtbody.Text = dal.GetBasicDataName(lblHurtbody.Text.Trim().Replace("&nbsp;", ""), "7");
            }

            Label lblLeval = (Label)e.Row.FindControl("lblLeval");
            if (lblHurtbody != null)
            {
                lblLeval.Text = dal.GetBasicDataName(lblLeval.Text.Trim().Replace("&nbsp;", ""), "10");
            }

            Label lblType = (Label)e.Row.FindControl("lblType");
            if (lblType != null)
            {
                lblType.Text = dal.GetBasicDataName(lblType.Text.Trim().Replace("&nbsp;", ""), "2");
            }

            Label lblCategory = (Label)e.Row.FindControl("lblCategory");
            if (lblCategory != null)
            {
                lblCategory.Text = dal.GetBasicDataName(lblCategory.Text.Trim().Replace("&nbsp;", ""), "38");
            }


        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sql = "";

        if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
        {
            ViewState["SortDirection"] = " desc";
        }
        else
            ViewState["SortDirection"] = "";

        sql = " order by " + e.SortExpression + ViewState["SortDirection"];


        DataBindToGridview(sql);
    }


    #endregion

    #region 分页需要
    public override void VerifyRenderingInServerForm(Control control)
    { }

    protected void PagerButtonClick(object sender, EventArgs e)
    {
        try
        {
            GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            DataBindToGridview("order by id desc");
        }
        catch
        {
            Response.Write("<script language='javascript'>alert('没有这么多页！');</script>");
            return;
        }
    }

    protected void btnReturn_Click1(object sender, EventArgs e)
    {
        try
        {
            if (GoPage.Text.Trim().ToString() == "")
            {
                Response.Write("<script language='javascript'>alert('页码不可以为空!');</script>");
            }
            else if (GoPage.Text.Trim().ToString() == "0" || Convert.ToInt32(GoPage.Text.Trim().ToString()) > GridView1.PageCount)
            {
                Response.Write("<script language='javascript'>alert('页码不是一个有效值!');</script>");
            }
            else if (GoPage.Text.Trim() != "")
            {
                int PageI = Int32.Parse(GoPage.Text.Trim()) - 1;
                if (PageI >= 0 && PageI < (GridView1.PageCount))
                {
                    GridView1.PageIndex = PageI;
                }
            }
            DataBindToGridview("order by id desc");
        }
        catch
        {
            DataBindToGridview("order by id desc");
            Response.Write("<script language='javascript'>alert('请输入有效的页码字符！');</script>");
        }
    }
    #endregion


    protected void drpType_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBindToGridview("order by id desc");
    }
}
